package org.monkey.common.cbs.entity.form;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import javax.persistence.MappedSuperclass;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.monkey.common.cbs.entity.mo.FieldFilter;
import org.monkey.common.cbs.entity.mo.FieldOrder;
import org.monkey.common.cbs.entity.mo.MybatisPlusQuery;

@Data
@Slf4j
@ApiModel("通用查询条件 仅限后期后管使用")
@MappedSuperclass
public class CommonListForm<T> extends BaseQueryForm<T> {
  @ApiModelProperty(name = "hump", value = "是否驼峰转换",example = "true")
  private boolean hump = true;

  @ApiModelProperty(name = "fieldFilters", value = "过滤字段 例如：筛选用户id 为1的记录")
  private List<FieldFilter> fieldFilters;

  @ApiModelProperty(name = "fieldOrders", value = "排序字段 有顺序的")
  private List<FieldOrder> fieldOrders;

  public MybatisPlusQuery<T> toMybatisPlusQuery() {
    MybatisPlusQuery<T> mybatisPlusQuery = new MybatisPlusQuery<>();
    QueryWrapper<T> rootQueryWrapper = new QueryWrapper<>();
    rootQueryWrapper = filterToWrapper(rootQueryWrapper, fieldFilters);
    rootQueryWrapper = orderToWrapper(rootQueryWrapper, fieldOrders);
    mybatisPlusQuery.setQueryWrapper(rootQueryWrapper);
    return mybatisPlusQuery;
  }
}
